package cn.com.wangluotongxin.netty.event;

import cn.com.wangluotongxin.admin.netty.constants.AdminNettyMsgType;
import cn.com.wangluotongxin.admin.netty.pojo.NotifyResponse;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class HeartBeatEventHandler implements ApplicationListener<HeartBeatEvent> {

    @Override
    public void onApplicationEvent(HeartBeatEvent event) {
        NotifyResponse response = new NotifyResponse()
                .setMsgType(AdminNettyMsgType.HEART_RESPONSE)
                .setChannelId(event.getChannel().id().asLongText())
                .setClientId(event.getClientId())
                .setDateTime(DateUtil.date());
        event.getChannel().writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(response)));
    }
}
